<?php declare(strict_types=1);
/**
 * @author      xianganyall <xianganyall@gmail.com>
 * @copyright   2023-2025 owner
 **/

namespace Srv\Apps\Smarty;

use Srv\Libs\Frame\EnvData;
use Srv\Libs\Frame\ResponseMain;
use Srv\Libs\Tools\Http;

final class SmartyLink
{
    /**
     * @return string
     * getUrlRoot
     */
    public static function getUrlRoot():string
    {
        $isSet          = false;
        $scheme         = strtolower(Http::getXClientScheme($isSet));
        $serverHost     = strtolower(Http::getXClientHost($isSet));
        if(strlen($serverHost) < 1) $serverHost = strtolower(strval(EnvData::getDataByServerItem(ResponseMain::SERVER_SERVER_NAME)));
        $serverPort     = intval(EnvData::getDataByServerItem(ResponseMain::SERVER_SERVER_PORT));
        if(strlen($serverHost) < 3) return '/';
        return ($scheme === 'https' ? 'https:' : 'http:').'//'.$serverHost.($serverPort < 1 || in_array($serverPort, [80, 443], true) || strpos($serverHost, ':') > 0 ? '' : ':'.$serverPort).'/';
    }

    /**
     * @param string $urlPath
     * @return string
     * getUrlWebRoot
     */
    public static function getUrlWebRoot(string $urlPath = ''):string
    {
        $rootPath       = EnvData::getRootPath();
        return self::getUrlRoot().(strlen($rootPath) > 0 ? $rootPath.'/' : '').ltrim($urlPath, '/');
    }

    /**
     * @param string $urlPath
     * @return string
     * getUrl
     */
    public static function getUrl(string $urlPath = ''):string
    {
        return self::getUrlRoot().ltrim($urlPath, '/');
    }
}